/**
 * Minecraft SoundsPack Core Library
 * Minecraft声音包核心库 - 用于管理和播放声音资源
 */

export type Category = "master" | "music" | "record" | "weather" | "block" | "hostile" | "neutral" | "player" | "ambient"
export interface SoundPackMcmeta {
    name: string;
    description?: string;
    version: number;
}

export interface Sound {
    name: string;
    volume?: number;
    pitch?: number;
    weight?: number;
    stream?: boolean;
    attenuation_distance?: number,
    preload?: boolean,
}

export interface SoundPackItem {
    keyValue: string;
    category: Category;
    replace?: boolean;
    subtitle?: string;
    sounds: Sound[]; // 这里我们需要确保类型是Sound而不是string
}

export interface SoundPackConfig {
    [key: string]: {
        category: Category;
        replace?: boolean;
        subtitle?: string;
        sounds: Sound[] | string[]; // 允许sounds为Sound[]或者string[]
    };
}

import { pinyin } from 'pinyin-pro';


class SoundPack {
    private name: string;
    private mcmeta = {
        pack: {
            pack_format: 1,
            description: ""
        }
    }
    private soundsConfig: SoundPackConfig = {};
    constructor(name: string, version: number = 1, description: string = "", author: string = "") {
        this.name = name;
        this.mcmeta.pack.pack_format = version;
        this.mcmeta.pack.description = author != "" ? description + "-" + author : description;
    }
    getMcmeta() {
        return this.mcmeta;
    }
    getName() {
        return this.name;
    }
    createSoundsConfig(keys: SoundPackItem[]): SoundPackConfig {
        const config: SoundPackConfig = {};
        keys.forEach((key) => {
            const { keyValue, category, replace, subtitle, sounds } = key;
            const pinyinRes = pinyin(this.name, { pattern: 'first', toneType: 'none', type: 'array' }).join("");
            config[pinyinRes.replace(/[^a-z0-9]/g, '') + "." + keyValue] = {
                category: category || 'record',
                replace: replace || true,
                subtitle: subtitle || "",
                sounds: sounds.map((sound: Sound) => {
                    // 当sound是Sound类型时，设置默认值
                    if (typeof sound === 'object' && sound !== null) {
                        return {
                            name: sound.name,
                            volume: sound.volume ?? 1,
                            pitch: sound.pitch ?? 1,
                            weight: sound.weight ?? 1,
                            stream: sound.stream ?? true,
                            attenuation_distance: sound.attenuation_distance ?? 16,
                            preload: sound.preload ?? false,
                        };
                    }
                    // 如果不是Sound类型，直接返回原始值（字符串）
                    return sound;
                }),
            };
        });
        return config;
    }
    
    /**
     * 设置声音列表
     * @param value 声音对象数组
     * @returns 处理后的声音对象数组
     */
    setSound(value: Sound[]) {
        // 处理每个声音对象
        value.forEach(sound => {
            // 移除.ogg后缀
            if (sound.name.endsWith('.ogg')) {
                sound.name = sound.name.substring(0, sound.name.length - 4);
            }
        });
        return value;
    }
}

export { SoundPack }
export default SoundPack
module.exports = {SoundPack}; 
module.exports = SoundPack; 